//
//  httpEngine.m
//  ILiangCang
//
//  Created by ZX on 16-4-15.
//  Copyright (c) 2016年 CRQ. All rights reserved.
//

#import "httpEngine.h"
#import "AFNetworking.h"
#import "dataModel.h"
#import "infosModel.h"
#import "shareModel.h"
#import "expertModel.h"
#import "listModel.h"


#define URL @"http://api.iliangcang.com/i/"
#define MAGAURL @"http://app.iliangcang.com/topic/"

@implementation httpEngine

#pragma mark ---------   商店

//获取商店首页
+(void)requestToGetShopMainMessageCompletion:(void (^)(NSMutableArray *, NSMutableArray *, NSError *))complete
{
    NSString * str = [NSString stringWithFormat:@"%@appshophome?app_key=iphone&build=170&osVersion=71&v=2.5.0",URL];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData * receiveData = responseObject;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableContainers error:nil];
        

        NSDictionary * slideDic = dic[@"slide"];
        NSArray * dataArray = slideDic[@"data"];
        NSMutableArray * scrollArray = [NSMutableArray array];
        for (NSDictionary * dict in dataArray)
        {
            listModel * data = [listModel listModelWithDictionary:dict];
            [scrollArray addObject:data];
        }
        
        NSArray * listArray = dic[@"list"][@"list"];
        NSMutableArray * otherArray = [NSMutableArray array];
        
        NSDictionary * smallDic = listArray[0];
        NSDictionary * oneDic = smallDic[@"one"];
        listModel * list = [listModel listModelWithDictionary:oneDic];
        [otherArray addObject:list];
        NSDictionary * twoDic = smallDic[@"two"];
        list = [listModel listModelWithDictionary:twoDic];
        [otherArray addObject:list];
        NSDictionary * thrDic = smallDic[@"three"];
        list = [listModel listModelWithDictionary:thrDic];
        [otherArray addObject:list];
        NSDictionary * forDic = smallDic[@"four"];
        list = [listModel listModelWithDictionary:forDic];
        [otherArray addObject:list];
        
        NSDictionary * small1Dic = listArray[1];
        NSDictionary * one1Dic = small1Dic[@"one"];
        list = [listModel listModelWithDictionary:one1Dic];
        [otherArray addObject:list];
        
        
        NSDictionary * small2Dic = listArray[2];
        NSDictionary * one2Dic = small2Dic[@"one"];
        list = [listModel listModelWithDictionary:one2Dic];
        [otherArray addObject:list];
        NSDictionary * two2Dic = small2Dic[@"two"];
        list = [listModel listModelWithDictionary:two2Dic];
        [otherArray addObject:list];
        
            
        
        complete (scrollArray,otherArray,nil);
       
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error)
        {
            complete(nil,nil,error);
            
        }

        
    }];
    
    
}


//获取商店专题
+(void)requestToGetShopTopicCompletion:(void (^)(NSMutableArray * topicArray, NSError * error))complete
{
    NSString * str = [NSString stringWithFormat:@"%@appshopmaga?app_key=iphone&build=170&osVersion=71&v=2.5.0",URL];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
        NSData * receiveData = responseObject;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableContainers error:nil];
         
 //        NSLog(@"%@",dic);
         NSArray * dataArray = dic[@"data"];
         NSMutableArray * completeArray = [NSMutableArray array];
         for (NSDictionary * dict in dataArray)
         {
             dataModel * data = [dataModel dataModelWithDictionary:dict];
             [completeArray addObject:data];
             
         }
         complete(completeArray,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error)
        {
            complete(nil,error);
        }

        
        
    }];
}


//获取商店品牌
+(void)requestToGetShopBrandWith:(NSInteger)page Completion:(void (^)(NSMutableArray *, NSError *))complete
{
    NSString * str = [NSString stringWithFormat:@"http://iliangcang.com:8200/brand/list/2?app_key=iphone&build=170&offset=10&osVersion=71&start=%ld&v=2.5.0",page];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData * receiveData = responseObject;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableContainers error:nil];
        NSDictionary * dataDic = dic[@"data"];
        NSArray * itemArray = dataDic[@"items"];
        NSMutableArray * completeArray = [NSMutableArray array];
        for (NSDictionary * dict in itemArray)
        {
            dataModel * data = [dataModel dataModelWithDictionary:dict];
            [completeArray addObject:data];
        }
        complete(completeArray,nil);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error)
        {
            complete(nil,error);
        }

        
    }];
    
    
}


//获取商店分类
+(void)requestToGetShopClassCompletion:(void (^)(NSMutableArray * classArray,NSMutableArray * childArray, NSError * error))complete
{
 //   NSString * str = [NSString stringWithFormat:@"http://iliangcang.com:8200/good/categories?app_key=Android&build=2016010501&build_channel=xiaomi"];
    
    
}


#pragma mark ---------   杂志

//获取杂志主页
+(void)requestToGetMagazineMainCompletion:(void (^)(NSMutableArray * sectionArray,NSMutableArray * rowArray, NSError * error))complete
{
    NSString * str = [NSString stringWithFormat:@"%@listinfo?app_key=iphone&build=170&osVersion=71&v=2.5.0",MAGAURL];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData * reciverData = responseObject;
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:reciverData options:NSJSONReadingMutableContainers error:nil];
        NSDictionary * dataDic = dic[@"data"];
        NSMutableArray * sectionArray = [NSMutableArray array];
        sectionArray = dataDic[@"keys"];
        
        NSDictionary * infosDic = dataDic[@"infos"];
        
        NSMutableArray * rowArray = [NSMutableArray array];
        //便利key值
        for (int i = 0;i < sectionArray.count ;i++)
        {
            NSString * key =sectionArray[i];
            NSMutableArray * smallArray = [NSMutableArray array];
            //通过key值   得到小数组
            NSArray *array=infosDic[key];
            //便利小数组   对infor进行解析
            for (int j=0;j<array.count;j++)
            {
                infosModel * info = [infosModel infosModelWithDictionary:array[j]];
                //                把每个区的infor加到小数组
                [smallArray addObject:info];
            }
            //每个小数组加入一个大的数组
            [rowArray addObject:smallArray];
            
            
        }
        
        
        complete(sectionArray,rowArray,nil);
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error)
        {
            complete(nil,nil,error);
        }

        
    }];
    
}


//获取TOP左页面
+(void)requestToGetMagazineLeftCompletion:(void (^)(NSMutableArray * leftArray, NSError * error))complete
{
    NSString * str = [NSString stringWithFormat:@"%@listcat?app_key=iphone&build=170&osVersion=71&v=2.5.0",MAGAURL];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData * reciverData = responseObject;
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:reciverData options:NSJSONReadingMutableContainers error:nil];
        NSArray * dataArray= dic[@"data"];
        NSMutableArray * leftArray = [NSMutableArray array];
        for (NSDictionary * dict in dataArray)
        {
            infosModel * info = [infosModel infosModelWithDictionary:dict];
            [leftArray addObject:info];
        }
        complete(leftArray,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error)
        {
            complete(nil,error);
        }
    }];
    
    
    
}

//获取TOP右页面
+(void)requestToGetMagazineRightCompletion:(void (^)(NSMutableArray * rightArray, NSError * error))complete
{
    NSString * str = [NSString stringWithFormat:@"%@listauthor?app_key=iphone&build=170&osVersion=71&v=2.5.0",MAGAURL];
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData * reciverData = responseObject;
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:reciverData options:NSJSONReadingMutableContainers error:nil];
        NSArray * dataArray= dic[@"data"];
        NSMutableArray * rightArray = [NSMutableArray array];
        for (NSDictionary * dict in dataArray)
        {
            infosModel * info = [infosModel infosModelWithDictionary:dict];
            [rightArray addObject:info];
        }
        complete(rightArray,nil);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error)
        {
            complete(nil,error);
        }
    }];

}



#pragma mark ---------   分享

//获取分享主页面
+(void)requestToGetShareMainWith:(NSInteger)page Completion:(void (^)(NSMutableArray *, NSError *))complete
{
    NSString * str = [NSString stringWithFormat:@"http://app.iliangcang.com/goods/class?app_key=iphone&build=170&count=12&osVersion=71&page=%ld&v=2.5.0",page];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData * reciverData = responseObject;
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:reciverData options:NSJSONReadingMutableContainers error:nil];
        NSArray * dataArray= dic[@"data"];
        NSMutableArray * shareArray = [NSMutableArray array];
        for (NSDictionary * dict in dataArray)
        {
            shareModel * share = [shareModel shareModelWithDictionary:dict];
            [shareArray addObject:share];
        }
        complete(shareArray,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error)
        {
            complete(nil,error);
        }
    }];
        

    
    
    
    
    
    
}
#pragma mark ---------   达人

//获取达人主页面
+(void)requestToGetExpertMainWith:(NSInteger )page Completion:(void (^)(NSMutableArray * expertArray, NSError * error))complete
{
    NSString * str = [NSString stringWithFormat:@"http://app.iliangcang.com/expert?app_key=iphone&build=170&count=12&osVersion=71&page=%ld&v=2.5.0",page];
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData * reciverData = responseObject;
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:reciverData options:NSJSONReadingMutableContainers error:nil];
        NSArray * dataArray= dic[@"data"];
        NSMutableArray * expertArray = [NSMutableArray array];
        for (NSDictionary * dict in dataArray)
        {
            expertModel * expert = [expertModel expertModelWithDictionary:dict];
            [expertArray addObject:expert];
        }
        complete(expertArray,nil);
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (error)
        {
            complete(nil,error);
        }
        
        
    }];
    
}

@end
